/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.util;

import junit.framework.TestCase;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.WriterAppender;

import java.util.List;

public class DiffTest extends TestCase {
  public static final Diff<String> DIFF = new Diff<String>(
      String.class, new String[]{
          "common1",
          "common2",
          "common3",
          "diff1Left",
          "diff2Left",
          "common4",
          "common5",
          "diff4Left",
          "diff5Left",
          "common6",
          "common7",
          "diff7Left",
          "common8",
      },

      new String[]{
          "common1",
          "common2",
          "common3",
          "diff1Right",
          "diff2Right",
          "diff3Right",
          "common4",
          "common5",
          "diff4Right",
          "diff5Right",
          "common6",
          "diff6Right",
          "common7",
          "common8",
          "diff8Right"
      });

  public void testRegularDiff() throws Exception {
    Logger.getRootLogger().addAppender(new WriterAppender(new SimpleLayout(), System.out));

    List<Diff.Segment<String>> results = DIFF.computeDifferences();
    assertNotNull(results);
    assertEquals(10, results.size());

    {
      Diff.Segment<String> result = results.get(0);
      assertTrue(result.isCommon());
      assertEquals(3, result.getCommonLines().length);
      assertEquals("common1", result.getCommonLines()[0]);
      assertEquals("common2", result.getCommonLines()[1]);
      assertEquals("common3", result.getCommonLines()[2]);
    }

    {
      Diff.Segment<String> result = results.get(1);
      assertFalse(result.isCommon());
      assertEquals(2, result.getLeftLines().length);
      assertEquals("diff1Left", result.getLeftLines()[0]);
      assertEquals("diff2Left", result.getLeftLines()[1]);
      assertEquals(3, result.getRightLines().length);
      assertEquals("diff1Right", result.getRightLines()[0]);
      assertEquals("diff2Right", result.getRightLines()[1]);
      assertEquals("diff3Right", result.getRightLines()[2]);
    }

    {
      Diff.Segment<String> result = results.get(2);
      assertTrue(result.isCommon());
      assertEquals(2, result.getCommonLines().length);
      assertEquals("common4", result.getCommonLines()[0]);
      assertEquals("common5", result.getCommonLines()[1]);
    }

    {
      Diff.Segment<String> result = results.get(3);
      assertFalse(result.isCommon());
      assertEquals(2, result.getLeftLines().length);
      assertEquals("diff4Left", result.getLeftLines()[0]);
      assertEquals("diff5Left", result.getLeftLines()[1]);
      assertEquals(2, result.getRightLines().length);
      assertEquals("diff4Right", result.getRightLines()[0]);
      assertEquals("diff5Right", result.getRightLines()[1]);
    }

    {
      Diff.Segment<String> result = results.get(4);
      assertTrue(result.isCommon());
      assertEquals(1, result.getCommonLines().length);
      assertEquals("common6", result.getCommonLines()[0]);
    }

    {
      Diff.Segment<String> result = results.get(5);
      assertFalse(result.isCommon());
      assertEquals(0, result.getLeftLines().length);
      assertEquals(1, result.getRightLines().length);
      assertEquals("diff6Right", result.getRightLines()[0]);
    }

    {
      Diff.Segment<String> result = results.get(6);
      assertTrue(result.isCommon());
      assertEquals(1, result.getCommonLines().length);
      assertEquals("common7", result.getCommonLines()[0]);
    }

    {
      Diff.Segment<String> result = results.get(7);
      assertFalse(result.isCommon());
      assertEquals(1, result.getLeftLines().length);
      assertEquals(0, result.getRightLines().length);
      assertEquals("diff7Left", result.getLeftLines()[0]);
    }

    {
      Diff.Segment<String> result = results.get(8);
      assertTrue(result.isCommon());
      assertEquals(1, result.getCommonLines().length);
      assertEquals("common8", result.getCommonLines()[0]);
    }

    {
      Diff.Segment<String> result = results.get(9);
      assertFalse(result.isCommon());
      assertEquals(0, result.getLeftLines().length);
      assertEquals(1, result.getRightLines().length);
      assertEquals("diff8Right", result.getRightLines()[0]);
    }
  }
}